//
//  WebServiceRequest.m
//  ERP
//
//  Created by goldeney on 15/7/29.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "WebServiceRequest.h"
#import "ServiceArgs.h"

@interface WebServiceRequest()

@property (nonatomic, strong) MBProgressHUD *mbProgressHUD;
@property (nonatomic, assign) BOOL showNetworkPrompt;

@property (nonatomic, strong) ServiceHelper *helper;
@property (nonatomic, strong) ServiceArgs *serviceArgs;

@property (nonatomic, strong) id showPromptView;
@property (nonatomic, strong) NSString *showPromptString;

@end

@implementation WebServiceRequest

- (id)init
{
    self = [super init];
    if (self) {
        self.helper = [[ServiceHelper alloc] init];
    }
    return self;
}

- (void)setServiceArgs:(ServiceArgs*)serviceArgs
{
    _serviceArgs = serviceArgs;
}

//设置显示连接提示框
- (void)setShowLoadingPrompt:(id)showView labelText:(NSString*)labelText
{
    self.showPromptView = showView;
    self.showPromptString = labelText;
}

#pragma mark 同步请求数据

- (ServiceResultModel*)startSyncConnect
{
    return [_helper syncService:self.serviceArgs];
}

#pragma mark 异步请求数据

-(void)startAsyncConnectCompleted:(finishBlockRequest)finish failed:(failedBlockRequest)failed abnormal:(abnormalBlockRequest)abnormal
{
    //加载进度条
    if (self.showPromptView)
    [self showMBProgressHUD:self.showPromptView labelText:self.showPromptString];
    
    [_helper asynService:self.serviceArgs completed:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(finish) finish(result,userInfo);
        //关闭进度条
        if (_mbProgressHUD){
            [_mbProgressHUD hide:YES];
            [_mbProgressHUD removeFromSuperview];
            _mbProgressHUD = nil;
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        if(failed) failed(error,userInfo);
        //关闭进度条
        if (_mbProgressHUD){
            [_mbProgressHUD hide:YES];
            [_mbProgressHUD removeFromSuperview];
            _mbProgressHUD = nil;
        }
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        if(abnormal) abnormal(error,userInfo);
        //关闭进度条
        if (_mbProgressHUD){
            [_mbProgressHUD hide:YES];
            [_mbProgressHUD removeFromSuperview];
            _mbProgressHUD = nil;
        }
    }];
}

#pragma mark -
#pragma mark MBProgressHUD

- (void)showMBProgressHUD:(id)anObject labelText:(NSString*)labelText
{
    if ([anObject isKindOfClass:[UIWindow class]]) {
        UIWindow *window = anObject;
        [self showMBProgressHUD:window frame:window.bounds labelText:labelText];
        
    } else if ([anObject isKindOfClass:[UINavigationController class]]) {
        UINavigationController *controller = anObject;
        [self showMBProgressHUD:controller.view frame:controller.view.bounds labelText:labelText];
        
    } else if ([anObject isKindOfClass:[UIViewController class]]) {
        UIViewController *controller = anObject;
        CGRect frame = CGRectMake(0, 0, controller.view.bounds.size.width, controller.view.bounds.size.height);
        [self showMBProgressHUD:controller.view frame:frame labelText:labelText];
        
    } else if ([anObject isKindOfClass:[UIView class]]) {
        UIView *view = anObject;
        [self showMBProgressHUD:view frame:view.bounds labelText:labelText];
    }
}

- (void)showMBProgressHUD:(UIView*)view frame:(CGRect)frame labelText:(NSString*)labelText
{
    if (!_mbProgressHUD) {
        _mbProgressHUD = [[MBProgressHUD alloc] initWithFrame:frame];
        [view addSubview:_mbProgressHUD];
        
        //如果设置此属性则当前的view置于后台
        _mbProgressHUD.dimBackground = YES;
        if (labelText) {
            _mbProgressHUD.labelText = labelText;
        } else {
            _mbProgressHUD.labelText = @"加载中";
        }
    }
    [_mbProgressHUD show:YES];
}

#pragma mark - 取消连接

- (void)cancel
{
    if(_helper) {
        [_helper.httpRequest cancel];
        [_helper.httpRequest clearDelegatesAndCancel];
    }
}

#pragma mark - 对象清理

- (void)dealloc
{
    if(_helper) {
        [_helper.httpRequest cancel];
        [_helper.httpRequest clearDelegatesAndCancel];
    }
    
    self.helper = nil;
    self.mbProgressHUD = nil;
}

@end
